<template>
	<view class="content">
		<nav-back></nav-back>
		<view class="welcome">
			<text>您好，
				欢迎加入暖余生</text>
		</view>
		<view class="ways">
			<view class="way" :class="activeWay=='code' ? 'active' : ''" @click="handleWays('code')">
				验证码登录
			</view>
			<view class="way" :class="activeWay=='password' ? 'active' : ''" @click="handleWays('password')">
				密码登录
			</view>
		</view>
		<view class="login">
			<form @submit="formSubmit" @reset="formReset">
				<view class="my-input username">
					<input class="uni-input" name="username" @input='handlePhone' placeholder="请输入手机号" />
				</view>
				<view class="my-input code" v-if="activeWay=='code'">
					<input class="uni-input" name="code" placeholder="验证码" />
					<text class="send" v-if="codeCd==0" @click="sendCode">发送验证码</text>
					<text class="send-cd" v-else="codeCd!=0">重新发送({{codeCd}}s)</text>
				</view>
				<view class="my-input password" v-if="activeWay=='password'">
					<input class="uni-input" password name="password" placeholder="请输入密码" />
				</view>
				<button form-type="submit" type="primary">立即登录</button>
			</form>
		</view>

		<view class="resiger" @click="handleResiger">
			去注册
			<image src="/static/img/arrow.png" mode="" />
		</view>
	</view>
</template>

<script>
	import request from '@/utils/request.js'
	import NavBack from '@/components/NavBack.vue'

	export default {
		components: {
			NavBack
		},
		data() {
			return {
				activeWay: 'code',
				isAgree: false,
				phone: '',
				codeCd: 0,
				phoneReg: ''
			};
		},
		onLoad() {
			this.phoneReg = /^1[3456789]\d{9}$/;
		},
		methods: {
			handleWays(type) {
				console.log(type, 'handleWays');
				this.activeWay = type
			},
			handlePhone(e) {
				// console.log(e.detail.value,'handlePhone');
				this.phone = e.detail.value
			},
			sendCode() {
				console.log(this.phone, 'sendCode');
				// 示例代码

				if (!this.phone) {
					uni.showToast({
						title: '请输入您的手机号',
						icon: 'none',
						duration: 2000
					})
					return
				}

				if (this.phoneReg.test(this.phone)) {
					console.log("手机号格式正确");
					this.codeCd = 60
					let timer = setInterval(() => {
						console.log('setInterval');
						this.codeCd = --this.codeCd
						if (this.codeCd == 0) {
							clearInterval(timer)
						}
					}, 1000)
					request(`/sms/send/${this.phone}`).then(((res) => {
						console.log(res, 'res');
					}))
				} else {
					console.log("手机号格式不正确");
					uni.showToast({
						title: '手机号格式错误',
						icon: 'none',
						duration: 1000
					})
				}
			},
			handleAgree() {
				this.isAgree = !this.isAgree
			},
			formSubmit(e) {
				const account = e.detail.value
				console.log('form发生了submit事件，携带数据为：', e.detail.value)
				if (this.activeWay == 'code' && (!account.username ||
						!account.code)) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none',
						duration: 2000
					})
					return
				} else if (this.activeWay == 'password' && (!account.username ||
						!account.password)) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none',
						duration: 2000
					})
					return
				}

				if (!this.phoneReg.test(this.phone)) {
					uni.showToast({
						title: '手机号格式错误',
						icon: 'none',
						duration: 2000
					})
					return
				}
				request(`/employee/login`, {
					method: 'POST',
					data: e.detail.value
				}).then(((res) => {
					let result = res.data.data
					console.log(result, 'result');
					if (res.data.code == 1) {
						uni.setStorageSync('id', String(result.id))
						uni.setStorageSync('token', result.token)
						uni.setStorageSync('userName', result.userName)
						uni.setStorageSync('name', result.name)
						uni.setStorageSync('avatar', result.avatar)
						if (result.isBaseInfo) {
							uni.redirectTo({
								url: '/pages/tabPage/home/home'
							})
						} else {
							uni.redirectTo({
								url: '/pages/userInfo/sexInfo/sexInfo'
							})
						}
					} else {
						console.log(res.data.msg, 'res');
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				}))
			},
			handleResiger() {
				uni.navigateTo({
					url: '/pages/userInfo/register/register'
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		flex-direction: column;
		padding: 20rpx 50rpx;
		box-sizing: border-box;
	}
	button[type=primary] {
		line-height: 90rpx !important;
	}
	.welcome {
		margin-top: 60rpx;

		text {
			font-size: 48rpx;
			font-weight: 700;
			line-height: 70rpx;
			color: #333333;

		}
	}

	.ways {
		display: flex;
		font-size: 32rpx;
		color: rgba(102, 102, 102, 1);
		margin-top: 80rpx;

		.way {
			margin-right: 65rpx;
		}

		.active {
			color: blue;
		}

		.active::after {
			clear: both;
			content: "";
			display: block;
			width: 85%;
			margin: 0 auto;
			margin-top: -12rpx;
			border-radius: 20rpx;
			background-color: rgba(156, 156, 255, 1);
			;
			height: 20rpx;
			overflow: hidden
		}
	}

	.login {
		margin-top: 60rpx;

		.code .send {
			color: rgba(107, 107, 255, 1);
			margin: 0 20rpx;
		}

		.code .send-cd {
			color: rgba(204, 204, 204, 1);
			margin: 0 20rpx;
		}

		.treaty-tip {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.treaty {
				justify-content: start;
				// background-color: rgb(143, 58, 58);
				// width: 60% !important;
				flex: 1;
			}

			.trouble {
				width: 30% !important;
				text-align: end;
				color: rgba(166, 166, 166, 1);
				font-size: 24rpx;
			}
		}
	}

	.resiger {
		display: flex;
		font-size: 28rpx;
		// background-color: rgb(175, 52, 52);
		align-items: center;
		color: rgba(107, 107, 255, 1);
		justify-content: center;
		margin-top: 200rpx;

		image {
			width: 24rpx;
			height: 20rpx;
			margin: 0 10rpx;
		}
	}
</style>